package com.chencl.cipher.controller;

import com.chencl.cipher.dto.ResponseBodyDTO;
import com.chencl.cipher.entity.SecretKeyEntity;
import com.chencl.cipher.service.SecretKeyService;
import com.chencl.cipher.util.RSAUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @program: rsa-aes-cipher-spring-boot
 * @description:
 * @author: jiangshengli
 * @create: 2022-03-18 09:36
 */
@Slf4j
@RestController
@RequestMapping("/secret")
@Validated
public class SecretKeyController {

    @Autowired
    private SecretKeyService secretKeyService;

    /**
     * 分配密钥
     */
    @PostMapping(value = "/addSecretKey")
    public ResponseBodyDTO addSecretKey(String appName){
        Map<String,String> resKey = RSAUtils.getRsaKey();
        String appId = secretKeyService.initAppid()+"";
        SecretKeyEntity secretKeyEntity = SecretKeyEntity.builder().appid(appId).appname(appName).publicKey(resKey.get("publiceKey")).privateKey(resKey.get("privateKey")).build();
        secretKeyService.insertSecretKey(secretKeyEntity);
        return ResponseBodyDTO.buildSuccess(secretKeyEntity);
    }
}
